package com.chinawayltd.ews.sample.manager.redis.impl;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.chinawayltd.ews.sample.manager.redis.MainAndMonitorDateManager;

/**
 * @program: greeProject-interfaceManager
 * @description 首页和监控大屏数据缓存
 * @author: chenxin
 * @create: 2018年5月22日 上午12:42:35
 */
@Service
public class MainAndMonitorDateManagerRedisImpl implements MainAndMonitorDateManager {

	private final String MAIN_PAGE = "MAIN_PAGE:";
    private final String REDIS_VEHICLE_DATA_BY_DAY = "REDIS_VEHICLE_DATA_BY_DAY";
    private final String REDIS_ALL_SCREEN_TASK = "REDIS_ALL_SCREEN_TASK";
    private final String REDIS_ALL_SCREEN_TASK_SIZE = "REDIS_ALL_SCREEN_TASK_NUM";
    private final String REDIS_TASK_ORDER_DETAIL = "REDIS_TASK_ORDER_DETAIL";
    private final int REDIS_VEHICLE_DATA_BY_DAY_INVALID_SECONDS = 60 * 60 * 24;
    private final int REDIS_ALL_SCREEN_TASK_INVALID_SECONDS = 60 * 5;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

	public void setVehicleDataByDay(String jsonString) {
		if(StringUtils.isBlank(jsonString)){
            throw new NullPointerException("data must be not blank!");
        }
		stringRedisTemplate.opsForValue().set(MAIN_PAGE+REDIS_VEHICLE_DATA_BY_DAY, jsonString,REDIS_VEHICLE_DATA_BY_DAY_INVALID_SECONDS, TimeUnit.SECONDS);
	}

	public Map<String, Object> getVehicleDataByDay() throws Exception{
		Object obj = stringRedisTemplate.opsForValue().get(MAIN_PAGE+REDIS_VEHICLE_DATA_BY_DAY);
        Map<String, Object> result = null;
        if(obj != null){
        	String data = obj.toString();
            result = JSONObject.parseObject(data);
        }else{
        	throw new NullPointerException("data must be not blank!");
        }
        return result;
	}

	public void setAllScreenTask(String jsonString) {
		if(StringUtils.isBlank(jsonString)){
            throw new NullPointerException("data must be not blank!");
        }
		stringRedisTemplate.opsForList().rightPush(MAIN_PAGE+REDIS_ALL_SCREEN_TASK, jsonString);
	}
	
	public String getAllScreenTask(int start, int end) {
		List<String> obj = stringRedisTemplate.opsForList().range(MAIN_PAGE+REDIS_ALL_SCREEN_TASK, start, end);
		String data = null;
		if(obj != null){
        	data = obj.toString();
        }
        return data;
	}
	
	public void setAllScreenTaskSize(int size) {
		//stringRedisTemplate.opsForValue().set(MAIN_PAGE+REDIS_ALL_SCREEN_TASK_SIZE, size+"",REDIS_ALL_SCREEN_TASK_INVALID_SECONDS, TimeUnit.SECONDS);
	}

	public int getAllScreenTaskSize() {
		Long size = stringRedisTemplate.opsForList().size(MAIN_PAGE+REDIS_ALL_SCREEN_TASK);
		return size.intValue();
	}

	public void setListOrderDetailByTaskId(String taskId, String jsonStr) {
		if(StringUtils.isBlank(jsonStr)){
            throw new NullPointerException("data must be not blank!");
        }
		stringRedisTemplate.opsForHash().put(MAIN_PAGE+REDIS_TASK_ORDER_DETAIL, taskId, jsonStr);
	}
	
	public String getListOrderDetailByTaskId(String taskId) {
		Object obj = stringRedisTemplate.opsForHash().get(MAIN_PAGE+REDIS_TASK_ORDER_DETAIL, taskId);
		String data = null;
		if(obj != null){
        	data = obj.toString();
        }
		return data;
	}

	public void deleteListOrderDetail() {
		stringRedisTemplate.delete(MAIN_PAGE+REDIS_TASK_ORDER_DETAIL);
	}

	public void deleteAllScreenTask() {
		stringRedisTemplate.delete(MAIN_PAGE+REDIS_ALL_SCREEN_TASK);
	}
}
